package ltd.newbee.mall.api.admin;

import ltd.newbee.mall.config.annotation.TokenToAdminUser;
import ltd.newbee.mall.entity.AdminUserToken;
import ltd.newbee.mall.entity.Menu;
import ltd.newbee.mall.service.impl.MenuService;
import ltd.newbee.mall.util.Result;
import ltd.newbee.mall.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/manage-api/v1")
public class MenuController {

    @Autowired
    private MenuService menuService;

    /**
     * 获取当前用户的菜单
     */
    @GetMapping("/menus")
    public Result getMenus(@TokenToAdminUser AdminUserToken adminUser) {
        try {
            Long userId = adminUser.getAdminUserId();
            List<Menu> menus = menuService.getMenuTreeByUserId(userId);
            return ResultGenerator.genSuccessResult(menus);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取菜单失败: " + e.getMessage());
        }
    }

    /**
     * 获取所有菜单（用于管理）
     */
    @GetMapping("/menus/all")
    public Result getAllMenus() {
        try {
            List<Menu> menus = menuService.getAllMenuTree();
            return ResultGenerator.genSuccessResult(menus);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取菜单列表失败: " + e.getMessage());
        }
    }

    /**
     * 根据角色ID获取菜单
     */
    @GetMapping("/menus/role/{roleId}")
    public Result getMenusByRoleId(@PathVariable Long roleId) {
        try {
            List<Menu> menus = menuService.getMenuTreeByRoleId(roleId);
            return ResultGenerator.genSuccessResult(menus);
        } catch (Exception e) {
            return ResultGenerator.genFailResult("获取角色菜单失败: " + e.getMessage());
        }
    }

    /**
     * 新增菜单
     */
    @PostMapping("/menus")
    public Result addMenu(@RequestBody Menu menu) {
        try {
            if (!StringUtils.hasText(menu.getMenuName())) {
                return ResultGenerator.genFailResult("菜单名称不能为空");
            }

            boolean success = menuService.addMenu(menu);
            if (success) {
                return ResultGenerator.genSuccessResult("添加成功");
            } else {
                return ResultGenerator.genFailResult("添加失败");
            }
        } catch (Exception e) {
            return ResultGenerator.genFailResult("添加菜单失败: " + e.getMessage());
        }
    }

    /**
     * 更新菜单
     */
    @PutMapping("/menus")
    public Result updateMenu(@RequestBody Menu menu) {
        try {
            if (menu.getMenuId() == null) {
                return ResultGenerator.genFailResult("菜单ID不能为空");
            }
            if (!StringUtils.hasText(menu.getMenuName())) {
                return ResultGenerator.genFailResult("菜单名称不能为空");
            }

            boolean success = menuService.updateMenu(menu);
            if (success) {
                return ResultGenerator.genSuccessResult("更新成功");
            } else {
                return ResultGenerator.genFailResult("更新失败");
            }
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新菜单失败: " + e.getMessage());
        }
    }

    /**
     * 删除菜单
     */
    @DeleteMapping("/menus/{menuId}")
    public Result deleteMenu(@PathVariable Long menuId) {
        try {
            boolean success = menuService.deleteMenu(menuId);
            if (success) {
                return ResultGenerator.genSuccessResult("删除成功");
            } else {
                return ResultGenerator.genFailResult("删除失败");
            }
        } catch (Exception e) {
            return ResultGenerator.genFailResult("删除菜单失败: " + e.getMessage());
        }
    }

    /**
     * 更新角色菜单权限
     */
    @PostMapping("/menus/role/{roleId}")
    public Result updateRoleMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        try {
            boolean success = menuService.updateRoleMenus(roleId, menuIds);
            if (success) {
                return ResultGenerator.genSuccessResult("权限更新成功");
            } else {
                return ResultGenerator.genFailResult("权限更新失败");
            }
        } catch (Exception e) {
            return ResultGenerator.genFailResult("更新权限失败: " + e.getMessage());
        }
    }
}
